﻿Public Class Group

    Public Cells As Cells
    Public Type, Index As Integer
    Public PossibleCells As SortedList(Of Char, Cells)
    Public Hints As New List(Of String)
    Public Reverse As Boolean

    Public Sub New(ByVal Type As Integer, ByVal Index As Integer, ByVal Cells As IEnumerable(Of Cell))
        Me.Type = Type
        Me.Index = Index
        Me.Cells = New Cells(Cells)
        Hint(0) = ""
    End Sub

    Property Hint(ByVal i As Integer) As String
        Get
            If i >= Hints.Count Then Return ""
            Return Hints(i)
        End Get
        Set(ByVal Value As String)
            If Value = "" Then
                If Hints.Count > 0 Then Hints.Remove(Hints.Last)
                Exit Property
            End If
            Do Until i < Hints.Count
                Hints.Add("")
            Loop
            Hints(i) = Value
        End Set
    End Property

    Public Sub New(ByVal Reverse As Boolean, ByVal Type As Integer, ByVal Index As Integer, ByVal Cells As Cells)
        Me.New(Type, Index, Cells)
        Me.Reverse = Reverse
        If Reverse Then Me.Cells.Reverse()
    End Sub

    'ReadOnly Property xIndexOf(ByVal Cell As Cell) As Integer
    '    Get
    '        Return Cells.IndexOf(Cell)
    '    End Get
    'End Property

    ReadOnly Property HasHint As Boolean
        Get
            Return Hints.Count > 0
        End Get
    End Property

    Overridable Sub ProcessHints()

    End Sub

    Overridable Function ProcessCombinations() As Boolean

    End Function

    ReadOnly Property Size As Integer
        Get
            Return Cells.Count
        End Get
    End Property

    Protected Pattern As String

    Overridable Sub Reset()
        PossibleCells = New SortedList(Of Char, Cells)
        Pattern = Cells(0).CandidateStr.Replace("*", "")
        PossibleCells.Clear()
        For Each ch In Pattern
            PossibleCells.Add(ch, New Cells(Cells.ToList))
        Next
    End Sub

    Sub Eliminate(ByVal Cell As Cell, ByVal ch As Char)
        If ch = "*" Then Exit Sub
        PossibleCells(ch).Remove(Cell)
    End Sub

    Sub Eliminate(ByVal Cell As Cell)
        For Each Item In PossibleCells.Values
            Item.Remove(Cell)
        Next
    End Sub

    Public Shared Operator >(ByVal g1 As Group, ByVal g2 As Group) As Boolean
        Return g1.Type = g2.Type AndAlso g1.Index > g2.Index
    End Operator

    Public Shared Operator <(ByVal g1 As Group, ByVal g2 As Group) As Boolean
        Return g1.Type = g2.Type AndAlso g1.Index < g2.Index
    End Operator

End Class


